package com.et114.components.mail;

import java.util.Map;
import java.util.List;
import java.io.File;


import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeBodyPart ;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.util.Assert;


import com.et114.core.common.AppContext;

/**
 * @author guanhw
 * SIMPALE
 */
public class DefaultMailService extends MailService implements InitializingBean {
	protected JavaMailSender mailSender;
	public void setMailSender( JavaMailSender mailSender ) {
		this.mailSender = mailSender;
	}


	public void send( SimpleMailMessage msg ) throws Exception {
		mailSender.send( msg );
	}



	public void send( SimpleMailMessage msg , String templateName , Map model  ) throws Exception {
		send ( msg , templateName , model , null ) ;
	}

	
	public void send( final SimpleMailMessage msg , String templateName , Map model , MailAttachment ma ) throws Exception {
		String content = generateEmailContent( templateName , model );
		sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
		MimeMessage mimeMsg;
		try {
			mimeMsg = mailSender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper( mimeMsg , true , AppContext.getFrameWorkPropertiesBean().getCharSet() );
			helper.setTo( msg.getTo() );
			helper.setSubject( msg.getSubject() );
			helper.setFrom( msg.getFrom() );
			helper.setText( content , true );
			
			if ( ma != null  ) {
			    String[] keys = ma.listKeys() ;
			    for  ( int i = 0 ; i < keys.length ; i ++ ) {
					String key = keys[ i ] ;
					File at = ma.getMailAttachment( key ) ; 
					MimeBodyPart attfile = new MimeBodyPart();
					FileDataSource fsr = new FileDataSource( at );
					attfile.setDataHandler( new DataHandler( fsr ) ) ; 
					attfile.setFileName ( "=?GBK?B?" + enc.encode( fsr.getName().getBytes() ) + "?=" ) ;
					attfile.setHeader ( "content-type" , "application/octet-stream" ) ;
					helper.getMimeMultipart ( ).addBodyPart ( attfile ) ;
			    }
			    
			    List ats = ma.getAttachments ( ) ; 
			    for  ( int i = 0 ; i < ats.size ( ) ; i ++ ) { 
			    	File at = ( File )ats.get ( i ) ; 
					MimeBodyPart attfile = new MimeBodyPart();
					FileDataSource fsr = new FileDataSource( at );
					attfile.setDataHandler( new DataHandler( fsr ) ) ; 
					attfile.setFileName ( "=?GBK?B?" + enc.encode( fsr.getName().getBytes() ) + "?=" ) ;
					attfile.setHeader ( "content-type" , "application/octet-stream" ) ;
					helper.getMimeMultipart ( ).addBodyPart ( attfile ) ;			    	
			    }
			}
			mailSender.send( mimeMsg ) ;
			
		} catch ( MessagingException ex ) {
			logger.error( ex.getMessage() , ex );
		}
	}
	
	public void afterPropertiesSet() throws Exception {
		Assert.notNull( mailSender , "mailSender" );
	}
}
